//
//  CreateNewAddressViewController.m
//  MobileShoppingMall
//
//  Created by 冯传昌 on 2021/4/27.
//  Copyright © 2021 冯传昌. All rights reserved.
//

#import "CreateNewAddressViewController.h"

#import "AddressCell.h"

#import "CGXPickerView.h"

@interface CreateNewAddressViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic,strong) AddressModel *currentAddressModel;

@property (nonatomic,strong) UITableView *tableView;

@property (nonatomic,strong) UIButton *saveBtn;

@end

@implementation CreateNewAddressViewController


- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    if (self.editStatus == YES) {
        self.currentAddressModel = self.editModel;
    }
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.topTitle = @"新建收货地址";
    self.view.backgroundColor = MainBackColor;
    self.currentAddressModel = [[AddressModel alloc] init];
    self.currentAddressModel.ID = 0;
    
    [self initUI];
}

#pragma mark - UI
-(void)initUI{
    [self.view addSubview:self.tableView];
    [self.view addSubview:self.saveBtn];
}

#pragma mark - lazy
- (UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, kHeight_NavBar, ScreenWidth, ScreenHeight-kHeight_NavBar-kSafe_Bottom-80) style:UITableViewStyleGrouped];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.backgroundColor = MainBackColor;
        [_tableView setTableHeaderView:[self tableHeaderFooterView]];
    }
    return _tableView;
}
-(UIView *)tableHeaderFooterView{
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, 0.1f)];
    return view;
}

- (UIButton *)saveBtn{
    if (!_saveBtn) {
        _saveBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _saveBtn.frame = CGRectMake(ScreenWidth/2-100, ScreenHeight-kSafe_Bottom - 70, 200, 40);
        _saveBtn.backgroundColor = RGB(71, 108, 255);
        _saveBtn.clipsToBounds = YES;
        _saveBtn.layer.cornerRadius = 4;
        [_saveBtn setTitle:@"保存" forState:normal];
        [_saveBtn setTitleColor:UIColor.whiteColor forState:normal];
        [_saveBtn addTarget:self action:@selector(saveClick) forControlEvents:UIControlEventTouchUpInside];
    }
    return _saveBtn;
}
#pragma mark - UITableViewDelegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 5;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row == 4) {
        return 86;
    }
    return 55;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    AddressCell *cell = [tableView dequeueReusableCellWithIdentifier:@"AddressCell"];
    if (!cell) {
        cell = [[AddressCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"AddressCell" withSourceType:indexPath.row];
    }
    WEAKSELF(self);
    [cell setUpdateAddressInfoBlock:^(AddressModel * _Nonnull model) {
        weakSelf.currentAddressModel = model;
        [weakSelf.tableView reloadData];
    }];
    [cell setSelectAddressBlock:^{
        [self selectAddressClick];
    }];
    cell.model = self.currentAddressModel;
    return cell;
}

#pragma mark - SEL
//选择地区
-(void)selectAddressClick{
    [self.view endEditing:YES];
    WEAKSELF(self);
    [CGXPickerView showAddressPickerWithTitle:@"请选择你的城市" DefaultSelected:@[@0, @0,@0] IsAutoSelect:YES Manager:nil ResultBlock:^(NSArray *selectAddressArr, NSArray *selectAddressRow) {
        weakSelf.currentAddressModel.province = selectAddressArr[0];
        weakSelf.currentAddressModel.city = selectAddressArr[1];
        weakSelf.currentAddressModel.region = selectAddressArr[2];
        [weakSelf.tableView reloadData];
    }];
}
-(void)saveClick{
    //保存 区分地址是新建还是修改
    if (self.currentAddressModel.linker.length <= 0 || [self.currentAddressModel.linker isEmpty] == YES) {
        [[XNProgressHUD shared] showWithTitle:@"请输入收货人姓名！"];
        return;
    }else if (self.currentAddressModel.linkphone.length <= 0 || [self.currentAddressModel.linkphone isEmpty] == YES) {
        [[XNProgressHUD shared] showWithTitle:@"请输入收货人手机号！"];
        return;
    }else if (self.currentAddressModel.province.length <= 0 || [self.currentAddressModel.province isEmpty] == YES) {
        [[XNProgressHUD shared] showWithTitle:@"请选择收货人所在地区！"];
        return;
    }else if (self.currentAddressModel.address.length <= 0 || [self.currentAddressModel.address isEmpty] == YES) {
        [[XNProgressHUD shared] showWithTitle:@"请选择收货人详细地址！"];
        return;
    }
    
    WEAKSELF(self);
    
    [NetWorkingAddress updateAddress:self.currentAddressModel WithSuccessBlock:^(id  _Nonnull responseObj) {
        if (self.editStatus == YES) {
            //修改 保存
            [[XNProgressHUD shared] showSuccessWithTitle:@"修改成功"];
        }else{
            //新建
            [[XNProgressHUD shared] showSuccessWithTitle:@"创建成功"];
        }
        
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [[XNProgressHUD shared] dismiss];
            [weakSelf.navigationController popViewControllerAnimated:YES];
            if (weakSelf.refreshAddressListBlock) {
                weakSelf.refreshAddressListBlock();
            }
        });
    }];
}


@end
